package your.energeia.namespace;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;


public class SurfaceLevel01Activity  extends Activity implements OnTouchListener{
	mainCharacter mainCharacter1;
	viewLevel01 mainView;
	Bitmap background;

	float posX = 0;
	float posY = 400;
	
	@Override 
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		mainView= new viewLevel01(this);
        mainView.setOnTouchListener(this);
		// requesting to turn the title OFF

		requestWindowFeature(Window.FEATURE_NO_TITLE);
       
		// making it full screen

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(mainView);
		
	}
	
	@Override
	public boolean onTouch(View mainView, MotionEvent event) {
		
		if (event.getAction() == MotionEvent.ACTION_UP) {
			
			   posX = event.getX();
			   posY = event.getY();			
		}
  
		return true;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mainView.pause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mainView.resume();
	}

	
	
	
	

	
	
	public class viewLevel01 extends SurfaceView implements Runnable{

		Thread principal = null;
		SurfaceHolder holder;
		boolean verificationFlag = false;
		public viewLevel01(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			holder =getHolder();
			//		droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);

			mainCharacter1 = new mainCharacter(BitmapFactory.decodeResource(getResources(),R.drawable.dog),0f,400f);
			background =BitmapFactory.decodeResource(getResources(), R.drawable.trangle);
		}



		public void run() {
			
	        while(verificationFlag==true){
	        	//perform canvas drawing
		if (!holder.getSurface().isValid()){
			
			continue;
			
		}
	//Lock Canvas
	Canvas can = holder.lockCanvas();
	//Draw onCanvas
	can.drawARGB(255,150,150,10);
    Update(can);

 	can.drawBitmap(background,100 ,250, null); 
	mainCharacter1.draw(can);
    //Colisiones();
	//Colisiones();
	//UnlockCanvas
	holder.unlockCanvasAndPost(can);
              }
        
        
        
        
		}
		
		public void pause(){
			
			verificationFlag = false;
			while(true){
				
				try{
					principal.join();
				}catch(InterruptedException e){
					e.printStackTrace();
				}
				break;
			}
			principal = null;
		}
		
		public void resume(){
			verificationFlag = true;
			principal = new Thread(this);
			principal.start();
			}
		
		}
	

	public void Update(Canvas can){
//if(mainCharacter1.colisiones(background)){

	mainCharacter1.update(posX, posY);

		//}
		
	
	
	/*public void Colisiones(Canvas can){
		
		*/if(mainCharacter1.colisiones(background)){

			can.drawARGB(255,150,150,10);
		}else{ 
				can.drawARGB(255,255,255,255);

		}
	/*}*/
	}
	
}

